package com.by.controller;


import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Employess;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controlller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.V;
import com.by.common.utils.Assert;
import com.by.query.EmployessQuery;
import com.by.service.EmployessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(tags = "管理员管理")
@RequestMapping("/api")
public class EmployessController extends BaseController {
  @Autowired
    EmployessService employessService;



  @ApiOperation("查询员工")
  @GetMapping("/employess")
  public  ResultBean<PageBean<List<Employess>>> get (@Validated(ISave.class) String employessId){
      System.out.println(employessId);
      Employess employess = employessService.get(employessId);
      return success("员工信息",employess);
  }
    /**
     * 新增员工
     * @param employess
     * @param result
     * @return
     */
    @ApiOperation("新增员工")
    @PostMapping("/employess")
    public ResultBean save(@Validated(ISave.class) @RequestBody Employess employess, BindingResult result){
        V.validate(result);
        int rs = employessService.save(employess);
        return saveResult(rs);
    }

    @ApiImplicitParam(name = "employessId", value="员工id",dataTypeClass = Integer.class ,paramType = "query",required = true)
    @ApiOperation("删除员工")
    @DeleteMapping("/employess")
    public ResultBean delete(@RequestParam(required = true) int employessId){
        int rs = employessService.delete(employessId);
        return deleteResult(rs);
    }



    @ApiOperation("修改员工")
    @PutMapping("/employess")
    public ResultBean update(@RequestBody Employess employess){
        int rs = employessService.update(employess);
        Assert.isZero(rs,"修改失败");
        return updateResult(rs);
    }

    @ApiOperation("查询员工列表")
    @PostMapping("/employess/list")
    public ResultBean<PageBean<List<Employess>>> query(@RequestBody EmployessQuery query) {
        PageBean bean = employessService.queryByMap(BeanUtil.beanToMap(query));
        return success("员工列表", bean);

}
}
